package main

import (
	"MaterialIdentification/svc"
	"github.com/gin-gonic/gin"
	"log/slog"
	"net/http"
)

type User struct {
	Id          uint
	Username    string
	Password    string
	Name        string
	Gender      string
	Phone       string
	Permissions string
}

func Login(context *gin.Context) {
	u := User{}

	err := context.BindJSON(&u)
	if err != nil {
		slog.Error("参数绑定错误", "err", err)
		return
	}

	model := User{}
	err = svc.Db.Where("username=? AND password=?", u.Username, u.Password).Find(&model).Error
	if model.Id == 0 {
		context.JSON(http.StatusOK, NewResp(1, "用户名或密码错误"))
		return
	}
	svc.Db.Where("username = ?", u.Username).First(&u)
	if err != nil {
		context.JSON(http.StatusOK, NewResp(1, "登录失败"))
		return
	}

	context.JSON(http.StatusOK, NewRespData("登录成功", u))
}
